/*
 * Copyright (C) 2024, KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 */

import QtQuick 2.12
import QtQml 2.15
import org.ukui.sidebar.ui 1.0
import org.ukui.sidebar.core 1.0
import org.ukui.quick.items 1.0 as UkuiItems
import org.ukui.quick.platform 1.0 as Platform

UkuiItems.StyleBackground {
    id: root
    readonly property int padding: mainWindow.isTabletMode ? 16 : 8
    paletteRole: Platform.Theme.Window
    radius: Platform.Theme.windowRadius

    function moveShortcutPanel(dx) {
        let newX = panelParent.width + padding - dx;
        panel.animationX = newX < 0 ? 0 : newX;
    }

    function initTabletProp() {
        panel.animationX = panelParent.width + padding;
    }

    function activePanelOnTablet(active) {
        if (panelAnimation.running) {
            return;
        }

        panelAnimation.active = active;
        panelAnimation.from = panel.animationX;
        panelAnimation.to = active ? 0 : (panelParent.width + root.padding);
        panelAnimation.start();
    }

    NumberAnimation {
        id: panelAnimation
        property bool active: true
        target: panel
        property: "animationX"
        duration: 300
        easing.type: Easing.InOutQuad

        onFinished: {
            if (!active) {
                mainWindow.hide();
            }
        }
    }

    MouseArea {
        anchors.fill: parent

        onClicked: {
            if (mainWindow.isTabletMode) {
                root.activePanelOnTablet(false);
            } else {
                //mainWindow.hide();
            }
        }
    }

    Binding {
        target: root
        property: "width"
        when: !mainWindow.isTabletMode
        value: panelParent.width + root.padding*2
        restoreMode: Binding.RestoreNone
    }

    Binding {
        target: root
        property: "height"
        when: !mainWindow.isTabletMode
        value: panelParent.height + root.padding*2
        restoreMode: Binding.RestoreNone
    }

    Item {
        id: panelParent

        z: 10
        x: mainWindow.isTabletMode ? (root.width - width - root.padding) : root.padding
        y: mainWindow.isTabletMode ? (root.height - height - root.padding) : root.padding
        width: panel.width
        height: panel.height

        ShortcuPanel {
            id: panel

            property real animationX: 0
            x: 0
            Binding on x {
                when: mainWindow.isTabletMode
                value: panel.animationX
                restoreMode: Binding.RestoreValue
            }

            width: mainWindow.isTabletMode ? 540 : 400
            height: childrenRect.height

            onXChanged: {
                let w = width + root.padding;
                mainWindow.setBlurStrength(Math.floor(4000*(1 - x/w)));
            }
        }
    }
}
